<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

/**
 * 用户收藏模型
 * @author Joker
 *        
 */
class UserCollect extends Model
{
    use SoftDeletes;
    
    protected $table='user_collect';
    
    protected $fillable = ['id','app','app_id','user_id'];
    
    protected $dates = ['deleted_at'];
    
    /**
     *  获取收藏列表，仅限 就业指南和充电加油
     * @param  $app 对应表名
     * @param  $user_id 当前用户编号
     */
    public function getList($app,$user_id)
    {
        $query = DB::table($app." as t")
                ->leftJoin('user_collect as uc','t.id','=','uc.app_id')
                ->leftJoin('users as u','t.user_id','=','u.id')
                ->leftJoin('category as c','t.cat_id','=','c.cat_id')
                ->leftJoin('attachment as a','t.thumb','=','a.aid');
        if($app=='employ')
        {
                $query->leftJoin('employ_ext as ee','ee.news_id','=','t.id')
                ->select('t.*','uc.created_at as utime','uc.id as collectid','u.name as username','c.cat_name','a.url as thumb','ee.*');
        }
        else 
        {
            $query->select('t.*','uc.created_at as utime','uc.id as collectid','u.name as username','c.cat_name','a.url as thumb');
        }
        return $query->where(array('uc.app'=>$app,'uc.user_id'=>$user_id,'uc.deleted_at'=>null))->paginate(10);
    }
    
    /**
     * 获取收藏的企业列表
     */
    public  function companyList($user_id,$app)
    {
        $result = DB::table('company as c')
                ->leftJoin('user_collect as uc','c.id','=','uc.app_id')
                ->leftJoin('company_address as ca','ca.company_id','=','c.id')
                ->leftJoin('company_scale as cs','c.scale_id','=','cs.id')
                ->leftJoin('company_develop as cd','c.develop_id','=','cd.id')
                ->select('c.*','uc.created_at as utime','uc.id as collectid','ca.address as address','cs.name as scalename','cd.name as developname')
                ->where(array('uc.app'=>$app,'uc.user_id'=>$user_id,'uc.deleted_at'=>null))
                ->paginate(10);
        return $result;
    }
    
    /**
     * 获取收藏的职位列表
     * @param integer $user_id
     */
    public function jobsList($user_id,$app)
    {
        $result = DB::table('company_jobs as cj')
                  ->leftJoin('user_collect as uc','cj.jobs_id','=','uc.app_id')
                  ->leftJoin('company as c','c.id','=','cj.company_id')
                  ->leftJoin('users as u','cj.user_id','=','u.id')
                  ->leftJoin('area as a','a.area_id','=','cj.area')
                  ->select('cj.*','c.name as companyname','a.title as areaname','u.name as username','uc.created_at as utime','uc.id as collectid')
                  ->where(array('uc.app'=>$app,'uc.user_id'=>$user_id,'uc.deleted_at'=>null))
                  ->paginate(10);
        return $result;
    }
    
    public function bbsList($user_id,$app)
    {
        $result = DB::table('user_collect as uc')
                  ->leftJoin('bbs_question as q','q.id','=','uc.app_id')
                  ->leftJoin('users as u','u.id','=','q.publish_uid')
                  ->select('uc.*','u.name as username','q.id as qid','q.title','q.created_at as qtime','q.focus_count','q.answer_count')
                  ->where(array('uc.app'=>$app,'uc.user_id'=>$user_id,'uc.deleted_at'=>null))
                  ->paginate(10);
        return $result;
    }
    
}